<?php
define('IN_DOUCO_API', true);
require (dirname(__FILE__) . '/include/init.php');

load()->func('app');

//微信支付回调页面
logging_run('微信支付',LOGGING_TRACE,'pay');
$notifyInfo = (array)simplexml_load_string(file_get_contents("php://input"), 'SimpleXMLElement', LIBXML_NOCDATA);
if (empty($notifyInfo)) {
    logging_run('Payment notification forbidden access.',LOGGING_ERROR,'error');
    return false;
}

if (empty($notifyInfo['sign'])) {
    logging_run('Payment notification signature is missing.' . var_export($notifyInfo, true),LOGGING_ERROR,'error');
    return false;
}
$data = $notifyInfo;
unset($data['sign']);

if ($notifyInfo['sign'] !== getPaySign($data, $_W['setting']['wxapp_signkey'])) {
    logging_run('Payment notification signature verification failed.' . var_export($notifyInfo, true),LOGGING_ERROR,'error');
    return false;
}


logging_run('Payment notification signature verification success.' . var_export($notifyInfo, true),LOGGING_INFO,'pay');

//支付成功
if($notifyInfo['result_code'] == 'SUCCESS' && $notifyInfo['return_code'] == 'SUCCESS') {
    //获取订单ID
    $order_sn = $notifyInfo['out_trade_no'];

    $order = pdo_get('order', array('order_sn' => $order_sn));


    if(($order['order_amount'] * 100) ==  $notifyInfo['total_fee']){
        //更改订单状态
        pdo_update('order',array('status'=>'1'),array('order_sn'=>$order_sn));
    }
    //返回给微信消息
    replyXml(array("return_code"=>'SUCCESS','return_msg'=>'DEAL WITH SUCCESS'));

}